package com.sqfw.project.learnRecord.controller;

import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.project.learnRecord.domain.LearnRecordComment;
import com.sqfw.project.learnRecord.service.ILearnRecordCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/learnRecord/comment")
public class LearnRecordCommentController extends BaseController {

    @Autowired
    private ILearnRecordCommentService learnRecordCommentService;

    /**
     * 通过录音id查询留言
     * @param recordId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('api:learnRecord:comment')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId){
        return success(learnRecordCommentService.selectLearnRecordCommentByRecordId(recordId));
    }


    /**
     * 新增录音留言
     * @param learnRecordComment
     * @return
     */
    @Log(title = "录音学习评论", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('api:learnRecord:comment')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LearnRecordComment learnRecordComment){
        //评论人对当前录音的收听时长超过改条录音时长才能评论
        boolean flag = learnRecordCommentService.canComment(learnRecordComment);
        if(!flag){
            return error("该条录音学习时间不足，无法评论");
        }
        return toAjax(learnRecordCommentService.insertLearnRecordComment(learnRecordComment));
    }
}
